home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / sep91.zip / CRNL.C < prev    next >
C/C++ Source or Header  |  1992-07-06  |  2KB  |  72 lines

  1. /*
  2.  *  crnl.c: Convert ASCII files in CR-NL (DOS) format to NL-only (UNIX) format.
  3.  *  usage:  crnl filename(s)
  4.  *  Compile:
  5.  *      cc crnl.c -o crnl
  6.  *
  7.  *      Written by Leor Zolman, 4/92/92, for inclusion with R&D Publications'
  8.  *      CUJ and Windows/DOS code disk archives on UUNET. Since these archives
  9.  *      are stored in MS-DOS file format, use this program to convert
  10.  *      individual text files into Unix text format.
  11.  *
  12.  *      DOS-format files are replaced with their UNIX-format equivalents.
  13.  *      I don't bother saving the original; if you want to preserve the
  14.  *      original files, make copies before running crnl on them.
  15.  */
  16.  
  17. #include <stdio.h>
  18.  
  19. main(argc, argv)
  20. int argc;
  21. char **argv;
  22. {
  23.     FILE *fp, *fpo;
  24.     int i, ch;
  25.     char *tmpname;
  26.     char command[100];
  27.     
  28.     if (argc < 2)
  29.         exit(fprintf(stderr, "usage:  %s filename(s)\n", argv[0]));
  30.     tmpname = tmpnam(NULL);
  31.     
  32.     for (i = 1; i < argc; i++)
  33.     {   
  34.         if ((fp = fopen(argv[i], "r")) == NULL)
  35.         {
  36.             fprintf(stderr, "%s: Can't open %s for input.\n", 
  37.                     argv[0],  argv[i]);
  38.             continue;
  39.         }
  40.  
  41.         printf("Converting %s...", argv[i]);
  42.         
  43.         if ((fpo = fopen(tmpname, "w")) == NULL)
  44.         {
  45.             fprintf(stderr, "%s: Can't open temp file %s for writing.\n", 
  46.                     argv[0],  tmpname);
  47.             fclose(fp);
  48.             exit(1);
  49.         }
  50.     
  51.         while ((ch = getc(fp)) != EOF)
  52.             if (ch != '\r')
  53.                 putc(ch, fpo);
  54.         
  55.         fclose(fp);
  56.         if (fclose(fpo))
  57.             exit(fprintf(stderr, "%s: error closing temp file %s.\n",
  58.                     argv[0], tmpname));
  59.  
  60.         unlink(argv[i]);
  61.  
  62.         sprintf(command, "cp %s %s", tmpname, argv[i]);
  63.         if (system(command))
  64.             exit(fprintf(stderr, "%s: error copying %s to %s.",
  65.                     argv[0], tmpname, argv[i]));
  66.  
  67.         unlink(tmpname);
  68.         printf("successful.\n");
  69.     }       
  70. }
  71.  
  72.